!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
recursive subroutine block_destroy (current, n_destroy_blocks)
!==================================================================================================================================|

   use parameters
   use block_define
   implicit none

   integer(4), intent(in) :: n_destroy_blocks
   type(block), pointer, intent(inout) :: current

   integer(4) :: i_neigh, j_neigh, i_block, j_block
   type(block), pointer :: child, neigh

!----------------------------------------------------------------------------------------------------------------------------------|
   if (current%nc .ne. 0 .and. current%lv .le. n_levels - 2) then
      do j_block = 1, current%nc_y
      do i_block = 1, current%nc_x
         child => current%child(i_block, j_block)%pblock
         if (child%nc .ne. 0) call block_destroy (child, 0)
      enddo
      enddo
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
!  reset links
!----------------------------------------------------------------------------------------------------------------------------------|
   do j_block = 1, current%nc_y
   do i_block = 1, current%nc_x
      child => current%child(i_block, j_block)%pblock
      do j_neigh = -1, 1
      do i_neigh = -1, 1
         neigh => child%neigh(i_neigh, j_neigh)%pblock
         if (.not. associated (neigh)) cycle
         if (neigh%lv .le. 0 .or. neigh%lv .eq. child%lv - 1) cycle
         if (any (neigh%parent%id .ne. child%parent%id)) then
            neigh%neigh(-i_neigh, -j_neigh)%pblock => current
         endif
      enddo
      enddo
   enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
!  release memory
!----------------------------------------------------------------------------------------------------------------------------------|
   do j_block = 1, current%nc_y
   do i_block = 1, current%nc_x
      child => current%child(i_block, j_block)%pblock
      if (child%var_allocate_flag) then
         deallocate(child%x, child%y,                                                                                              &
            child%ro, child%mx, child%my, child%mz, child%bx, child%by, child%bz, child%en, child%gx, child%gy, child%po, child%az)
         deallocate(child%fxro_bnd, child%fxmx_bnd, child%fxmy_bnd, child%fxmz_bnd,                                                &
                    child%fxbx_bnd, child%fxby_bnd, child%fxbz_bnd, child%fxen_bnd,                                                &
                    child%fyro_bnd, child%fymx_bnd, child%fymy_bnd, child%fymz_bnd,                                                &
                    child%fybx_bnd, child%fyby_bnd, child%fybz_bnd, child%fyen_bnd,                                                &
                    child%fxpo_bnd, child%fypo_bnd)
         child%var_allocate_flag = .false.
         if (ini_value_save_flag .ne. 0) then
            deallocate (child%ro0, child%mx0, child%my0, child%mz0, child%bx0, child%by0, child%bz0, child%en0, child%az0)
         endif
      endif
      deallocate (child)
   enddo
   enddo

   if (associated (current%child)) deallocate (current%child)

   n_blocks(cpu_id_p1) = n_blocks(cpu_id_p1) - n_destroy_blocks
   current%nc_x = 0
   current%nc_y = 0
   current%nc = 0

!----------------------------------------------------------------------------------------------------------------------------------|

   return
end subroutine block_destroy

